from exts import db
from datetime import datetime


class DishType(db.Model):
    __tablename__='dishType'
    dish_type_no=db.Column(db.Integer,primary_key=True,autoincrement=True)
    dish_type_name = db.Column(db.String(100),nullable=False)
    on_time = db.Column(db.DateTime,default=datetime.now)
    is_delete = db.Column(db.Boolean,default=False)

    dishes = db.relationship("Dish",back_populates='dishType')

# Order = db.Table(
#     "order",
#     db.Column('order_no',db.Integer,primary_key=True,autoincrement=True),
#     db.Column("order_time",db.DateTime,default=datetime.now),
#     db.Column('is_done',db.Boolean,default=False),
#     db.Column("dish_no",db.Integer, db.ForeignKey('dish.dish_no')),
#     db.Column('desk_no',db.Integer, db.ForeignKey("desk.desk_no"))
# )

order_dish_table = db.Table('order_dish_table',
                            db.Column('order_no',db.Integer,db.ForeignKey('order.order_no'),primary_key=True),
                            db.Column('dish_no',db.Integer,db.ForeignKey('dish.dish_no'),primary_key=True))


class Order(db.Model):
    __tablename__ ='order'
    order_no = db.Column(db.Integer,primary_key=True,autoincrement=True)
    order_time = db.Column(db.DateTime,default=datetime.now)
    is_done = db.Column(db.Boolean,default=False)

    desk_no = db.Column(db.Integer,db.ForeignKey('desk.desk_no'))
    desk = db.relationship('Desk',back_populates='orders')

    dishes = db.relationship('Dish',secondary=order_dish_table,back_populates='orders')


class Dish(db.Model):
    __tablename__='dish'
    dish_no = db.Column(db.Integer,primary_key=True,autoincrement=True)
    dish_name = db.Column(db.String(100),nullable=True)
    price = db.Column(db.DECIMAL(20,5),nullable=False)
    on_time = db.Column(db.DateTime,default=datetime.now)
    is_delete = db.Column(db.Integer,default=False)
    dish_type_no = db.Column(db.Integer,db.ForeignKey('dishType.dish_type_no'))

    dishType = db.relationship('DishType',back_populates='dishes')
    # desks = db.relationship("Desk",secondary=Order,back_populates='dishes')

    orders = db.relationship('Order',secondary=order_dish_table,back_populates='dishes')

class Desk(db.Model):
    __tablename__ ='desk'
    desk_no = db.Column(db.Integer,primary_key=True,autoincrement=True)
    desk_name = db.Column(db.String(100),nullable=False)
    men = db.Column(db.Integer,nullable=False)
    on_time = db.Column(db.DateTime,default=datetime.now)
    is_delete = db.Column(db.Boolean,default=False)
    # dishes = db.relationship('Dish',secondary=Order,back_populates='desks')

    orders = db.relationship('Order',back_populates='desk')
